package com.situ.car.dao.Impl;

import com.situ.car.dao.IReturnDao;
import com.situ.car.pojo.entity.RReturn;
import com.situ.car.pojo.query.ReturnQuery;
import com.situ.car.pojo.vo.ReturnVo;
import com.situ.car.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ReturnDaoImpl implements IReturnDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public int selectTotalCount(ReturnQuery returnQuery) {
        String sql = "select count(*) \n" +
                "FROM rent_return AS rr\n" +
                "JOIN rental_order AS r ON rr.rental_id = r.id\n" +
                "JOIN car AS c ON r.car_id = c.id\n" +
                "JOIN user AS u ON r.user_id = u.id where 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryOrderNumber = returnQuery.getOrderNumber();
        if (!ObjectUtils.isEmpty(queryOrderNumber)) {
            sql += " and r.order_number like ?";
            queryList.add("%" + queryOrderNumber + "%");
        }
        String queryLicensePlate = returnQuery.getLicensePlate();
        if (!ObjectUtils.isEmpty(queryLicensePlate)) {
            sql += " and c.license_plate like ?";
            queryList.add("%" + queryLicensePlate + "%");
        }
        String queryUsername = returnQuery.getUsername();
        if (!ObjectUtils.isEmpty(queryUsername)) {
            sql += " and u.username like ?";
            queryList.add("%" + queryUsername + "%");
        }
        Date queryBeginCreateTime = returnQuery.getBeginCreateTime();
        Date queryEndCreateTime = returnQuery.getEndCreateTime();
        if (!ObjectUtils.isEmpty(queryBeginCreateTime)&&!ObjectUtils.isEmpty(queryEndCreateTime)) {
            sql += " and rr.return_date between ? and ?";
            queryList.add(queryBeginCreateTime);
            queryList.add(queryEndCreateTime);
        }
        Integer count = template.queryForObject(sql, Integer.class, queryList.toArray());
        return count;
    }

    @Override
    public List<ReturnVo> selectByPage(ReturnQuery returnQuery) {
        String sql = "SELECT \n" +
                "    rr.id AS id,\n" +
                "    r.order_number AS orderNumber,\n" +
                "    c.model AS model,\n" +
                "    c.license_plate AS licensePlate,\n" +
                "    u.username AS username,\n" +
                "    rr.damage_description AS damageDescription,\n" +
                "    rr.return_date AS returnDate\n" +
                "FROM rent_return AS rr\n" +
                "JOIN rental_order AS r ON rr.rental_id = r.id\n" +
                "JOIN car AS c ON r.car_id = c.id\n" +
                "JOIN user AS u ON r.user_id = u.id where 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryOrderNumber = returnQuery.getOrderNumber();
        if (!ObjectUtils.isEmpty(queryOrderNumber)) {
            sql += " and r.order_number like ?";
            queryList.add("%" + queryOrderNumber + "%");
        }
        String queryLicensePlate = returnQuery.getLicensePlate();
        if (!ObjectUtils.isEmpty(queryLicensePlate)) {
            sql += " and c.license_plate like ?";
            queryList.add("%" + queryLicensePlate + "%");
        }
        String queryUsername = returnQuery.getUsername();
        if (!ObjectUtils.isEmpty(queryUsername)) {
            sql += " and u.username like ?";
            queryList.add("%" + queryUsername + "%");
        }
        Date queryBeginCreateTime = returnQuery.getBeginCreateTime();
        Date queryEndCreateTime = returnQuery.getEndCreateTime();
        if (!ObjectUtils.isEmpty(queryBeginCreateTime)&&!ObjectUtils.isEmpty(queryEndCreateTime)) {
            sql += " and rr.return_date between ? and ?";
            queryList.add(queryBeginCreateTime);
            queryList.add(queryEndCreateTime);
        }
        sql += " limit ?,?";
        int offset = (returnQuery.getPage() - 1) * returnQuery.getLimit();
        queryList.add(offset);
        queryList.add(returnQuery.getLimit());
        List<ReturnVo> list = template.query(sql, new BeanPropertyRowMapper<>(ReturnVo.class), queryList.toArray());
        return list;
    }

    @Override
    public void add(RReturn rReturn) {
        template.update("INSERT INTO rent_return (rental_id, damage_description) VALUES (?,?)", rReturn.getRentalId(), rReturn.getDamageDescription());
    }

    @Override
    public void deleteById(Integer id) {
        template.update("delete from rent_return where id=?", id);
    }

    @Override
    public ReturnVo selectById(Integer id) {
        return template.queryForObject("SELECT \n" +
                "    rr.id AS id,\n" +
                "    r.order_number AS orderNumber,\n" +
                "    rr.damage_description AS damageDescription\n" +
                "FROM rent_return AS rr\n" +
                "JOIN rental_order AS r ON rr.rental_id = r.id where rr.id=?", new BeanPropertyRowMapper<>(ReturnVo.class), id);
    }

    @Override
    public void update(RReturn rReturn) {
        template.update("update rent_return set damage_description=? where id=?"
                ,rReturn.getDamageDescription(),rReturn.getId());
    }

    @Override
    public RReturn selectByRentId(Integer rentalId) {
        return template.queryForObject("SELECT * FROM rent_return where rental_id=?", new BeanPropertyRowMapper<>(RReturn.class), rentalId);
    }
}
